package com.itheima.dao.Impl;

import com.itheima.dao.UserDao;
import com.itheima.domain.User;
import com.itheima.utils.JDBCUtils;

import java.sql.*;
import java.util.ArrayList;
import java.util.List;

public class UserDaoImpl implements UserDao {
    Connection conn = null;
    Statement st = null;
    ResultSet rs = null;
    ArrayList<User> list = new ArrayList<>();

    @Override
    public List<User> findAllUser() {
        try {
            conn = JDBCUtils.getConnection();
            st = conn.createStatement();
            String sql = "select * from user";
            rs = st.executeQuery(sql);
            while (rs.next()) {
                User user = new User(
                        rs.getInt("id"),
                        rs.getString("name"),
                        rs.getString("gender"),
                        rs.getInt("age"),
                        rs.getString("address"),
                        rs.getString("qq"),
                        rs.getString("email")
                );
                list.add(user);
            }

        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            JDBCUtils.close(rs, st, conn);
        }
        return list;
    }

    @Override
    public void adduser(User user) {
        Connection conn = JDBCUtils.getConnection();
        String sql = "INSERT  INTO user VALUES (null,'" + user.getName() + "','" + user.getGender() + "'," + user.getAge() + ",'" + user.getAddress() + "','" + user.getQq() + "','" + user.getEmail() + "')";
        try {
            Statement st = conn.createStatement();
            st.executeUpdate(sql);
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            JDBCUtils.close(st, conn);
        }
    }

    @Override
    public void deleteById(String id) {
        Connection conn = JDBCUtils.getConnection();
        String sql = "delete from user where id=" + id;
        try {
            Statement st = conn.createStatement();
            st.executeUpdate(sql);
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            JDBCUtils.close(st, conn);
        }
    }

    @Override
    public User findById(String id) {
        Connection conn = null;
        Statement st = null;
        ResultSet rs = null;
        User user = null;
        try {
            conn = JDBCUtils.getConnection();
            String sql = "select * from user where id=" + id;
            st = conn.createStatement();
            rs = st.executeQuery(sql);
            if (rs.next()) {
                user = new User(rs.getInt("id"),
                        rs.getString("name"),
                        rs.getString("gender"),
                        rs.getInt("age"),
                        rs.getString("address"),
                        rs.getString("qq"),
                        rs.getString("email"));
            }
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            JDBCUtils.close(rs, st, conn);
        }
        return user;
    }

    @Override
    public void updateUser(User user) {
        Statement statement = null;
        Connection conn = null;
        try {
            conn = JDBCUtils.getConnection();
            String sql = "update user set name = '" + user.getName() + "',gender = '" + user.getGender() + "' ,age = " + user.getAge() + " , address = '" + user.getAddress() + "' , qq = '" + user.getQq() + "', email = '" + user.getEmail() + "' where id = " + user.getId();
            statement = conn.createStatement();
            statement.executeUpdate(sql);
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }
}
